package com.bingo.framework.util;

import java.util.HashMap;
import java.util.Map;

import com.fasterxml.jackson.core.JsonProcessingException;

public class ResultTools {
	public static final String result_success = "success";
	public static final String result_failure = "failure";
	public static final String key_result = "result";
	public static final String key_message = "message";
	public static final String key_content = "content";
	public static final String defaultError = "{\"result:\", \"failure\", \"message\", \"jsonParseError\"}";
	
	public static String errorResult(String errorMsg) {
		Map<String, Object> map = getDataResult();
		map.put(key_result, result_failure);
		map.put(key_message, errorMsg);
		return map2str(map);
	}
	
	public static String illegalParamResult(String param) {
		Map<String, Object> map = getDataResult();
		map.put(key_result, result_failure);
		map.put(key_message, "param "+param+" is illegal");
		return map2str(map);
	}
	
	public static String successResult() {
		return map2str(getDataResult());
	}
	
	public static String successResult(String message) {
		Map<String, Object> map = getDataResult();
		map.put(key_message, message);
		return map2str(map);
	}
	
	public static String dataResult(Object data) {
		Map<String, Object> map = getDataResult();
		map.put(key_content, data);
		return map2str(map);
	}
	
	private static Map<String, Object> getDataResult() {
		Map<String, Object> map = new HashMap<>();
		map.put(key_result, result_success);
		return map;
	}
	
	private static String map2str(Map<String, Object> map) {
		String result  = defaultError;
		try {
			result = JsonTools.obj2Str(map);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return result;
	}
}
